//
// Created by cube on 2020/10/26.
//
#include <gtest/gtest.h>

#include "../CommonNetBase/Controller.h"
#include "../../lib/Common/Pools.h"
#include "../../lib/Common/CThread.h"
#include "../../lib/Base/AOP_IOC.h"
#include "../UdpPool.h"
#include "../CRUDP.h"

#include <string>

using namespace Lib;
using namespace NetBaseCommon;

#include <thread>
#include <iostream>
#include <tuple>

class MMM : public UdpPool{
public:
    using UdpPool::UdpPool;
protected:
    int read_from_io(NetUdp& udp,NetAddress& addr) override {
        char buf[1024] = {};
        int rul = udp.recv(addr,(int8_t*)buf,sizeof(buf));
        std::cout<<"read  "<<udp.getSocket()<<"  "<<buf<<std::endl;
        std::string send_buf = "hello client. im server ";
        send(addr,(int8_t*)send_buf.data(),send_buf.size());
        return rul;
    }
};

TEST(CRUDP_TEST,UdpPool_Server_TEST){
    MMM udps(10);
    udps.bind(NetAddress("127.0.0.1",8822));
    udps.bind(NetAddress("127.0.0.1",8811));
    udps.bind(NetAddress("127.0.0.1",8833));
    std::thread t([&](){
        while (udps.server() != -1);
    });
    udps.ubind(NetAddress("127.0.0.1",8833));
    t.join();
}

class CommandParse{
public:

    bool parse(int8_t* data,std::size_t len,std::size_t& index,uint8_t& id,uint8_t*& cmd){

    }

};

// |id|size|len|...|len|...|id|size|...|...|id|size|...c...|
// |id|...|

class Command{
public:
    union Slip{
        uint32_t    len;
        uint8_t     flen;
        char        c;
    };
public:
    Command(uint32_t id,uint32_t index,uint32_t len,Slip slip)
    :__id(id),__index(index),__len(len),__slip(slip){}



private:
    uint32_t    __id;
    uint32_t    __index;
    uint32_t    __len;
    Slip        __slip;
};

TEST(LL,LLA){
    std::cout<<"sss"<<std::endl;
    CRUDPA crudpa(10);
//    std::string buf = "";
//    crudpa.bind(NetAddress(INADDR_ANY,9900));
//    crudpa.setTimeOut([&](const NetAddress& addr){
//        std::cout<<addr.getIp_Char()<<"  "<<addr.getPort()<<std::endl;
//    });
//    crudpa.setRecv([&](const NetAddress& addr){
//        std::cout<<addr.getIp_Char()<<"  "<<addr.getPort()<<"  "<<std::endl;
//        crudpa.recv(addr,(int8_t*)buf.data(), 1024);
//        std::cout<<buf<<std::endl;
//        buf = "hello my is server!";
//        crudpa.send(addr,(int8_t*)buf.data(),buf.length());
//    });
//    crudpa.loop()->exec();
}

